<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-23
 * Time: 15:44
 * 创建订单
 */

namespace app\api\service\store;


use app\api\model\Address;
use app\api\model\Admin;
use app\api\model\PickDeliverStaff;
use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\service\coupon\CouponOrderSer;
use app\api\service\message\MessageSer;
use app\api\service\Token;
use app\api\validate\CheckRemark;
use app\api\validate\order\CheckStoreOrder;
use app\lib\exception\ParameterException;

class StoreOrderSer extends Token
{
    /**
     * 1.添加用户ID
     * 2.判断传入信息
     * 3.地址ID 判断地址信息
     * 4.服务门店id 判断服务门店ID
     * 5.创建订单
     * 6.返回支付number
     */

    // 用户id
    private $uid;

    // post信息
    private $postArr;

    // 门店信息
    private $admin;

    function __construct($postArr)
    {
        (new CheckRemark())->goCheck();
        $this->postArr = $postArr;
        $this->uid = self::getCurrentUid();
    }

    // 入口方法
    public function entrance()
    {
        (new CheckStoreOrder())->goCheck();
        return $this->judgeData();
    }

    // 判断订单
    private function judgeData()
    {
        $info = StoreOrder::where('user_id',$this->uid)->where('status','lt',6)->find();
        if($info) {
            throw new ParameterException([
                'msg' => [
                    'address' => '您已经有正在进行中的订单'
                ]
            ]);
        }
        $address = Address::where('id', $this->postArr['address_id'])
            ->where('user_id', $this->uid)->find();
        if (!$address) {
            throw new ParameterException([
                'msg' => [
                    'address' => '地址信息查询错误'
                ]
            ]);
        }
        $this->admin = Admin::with('adminUser')->where('id', $this->postArr['store_id'])->find();
        if (!$this->admin) {
            throw new ParameterException([
                'msg' => [
                    'address' => '门店信息查询错误'
                ]
            ]);
        }
        $this->postArr['name'] = $address['name'];
        $this->postArr['mobile'] = $address['mobile'];
        $this->postArr['address'] = $address['province'] . $address['city'] . $address['county'] . $address['address'];
        $this->postArr['store_name'] = $this->admin['admin_user']['username'];
        $this->postArr['store_mobile'] = $info['phone'];
        return $this->createOrder();
    }

    // 创建订单
    private function createOrder()
    {
        $user = User::get($this->uid);
        $mode = StoreOrder::create([
            'user_id' => $this->uid,
            'user_grade_name' => $user['grade_name'],
            'name' => $this->postArr['name'],
            'mobile' => $this->postArr['mobile'],
            'address' => $this->postArr['address'],
            'admin_id' => $this->postArr['store_id'],
            'lin_user_id' => $this->admin['lin_user_id'],
            'store_name' => $this->postArr['store_name'],
            'store_mobile' => $this->admin['phone'],
            'take_time' => $this->postArr['take_time'],
            'remark' => $this->postArr['remark'],
            'order_no' => self::makeOrderNo()
        ]);
        $out_trade_no = '2zheng2020'.$mode->id;
        $mode->out_trade_no = $out_trade_no;
        $mode->save();
        if($this->postArr['coupon_id'] > 0) {
            $couponOrder = new CouponOrderSer(1,$this->postArr['coupon_id'],$mode->id);
            $couponOrder->entrance();
        }
        $this->setMessage();
        return writeJson(201, [ 'out_trade_no'=>$out_trade_no ], '订单创建成功！');
    }


    // 取送员推送模板消息
    private function setMessage()
    {
        $message = new MessageSer();
        $keyword1 = $this->postArr['name'];
        $keyword2 = $this->postArr['mobile'];
        $keyword3 = $this->postArr['address'];
        $keyword4 = date('Y-m-d H:i:s',time());
        $keyword5 = $this->postArr['take_time'];
        $remark = '点击进行去抢单';
        $list = PickDeliverStaff::where('lin_user_id',$this->admin['lin_user_id'])
            ->with('user')->select()->toArray();
        foreach ($list as $v) {
            if($v['user']['wechat_openid']) {
                $message->index_one(
                    $v['user']['wechat_openid'],
                    $v['name'].'您好，门店有新的订单',
                    $keyword1,
                    $keyword2,
                    $keyword3,
                    $keyword4,
                    $keyword5,
                    $remark,
                    ''
                );
            }
        }
    }
}